define([],function(){
  let opt = {
    'code:select' : function(data){
        this.$root.row.scancode = data.value;
        //查找对应的产品信息，然后显示到页面
        this.$http.post('?controller=Sample_Ruku&action=GetRk2ProInfo',{'scancode':data.value,'kindId':this.$root.row.kindId,'kind':data.kind}).then((res)=>{
            if(!res.data.success) {
                this.$notify.error({
                  'title':'获取产品失败',
                  'message':res.data.msg ? res.data.msg : '获取产品信息失败'
                });

                return false;
            }
            // 赋值
            this.$root.row.proName = res.data.data.proName || '';
            this.$root.row.proCode = res.data.data.proCode || '';
            this.$root.row.gexing = res.data.data.gexing || '';
            this.$root.row.zuzhi = res.data.data.zuzhi || '';
            this.$root.row.proDesc = res.data.data.proDesc || '';
            this.$root.row.type = res.data.data.type || '';
            this.$root.row.proType = res.data.data.proType || '';
            this.$root.row.hzlId = res.data.data.hzlId || '';
            if(this.$root.row.proType){
                this.$root.row.productId = res.data.data.productId || '';
            }else{
                this.$root.row.productId = res.data.data.id || '';
            }
            for (var i = 0; i < res.data.columns.length; i++) {
            	var column = res.data.columns[i];
                this.$set(this.$root.row,[column],res.data.data[column]);
            }
        }).catch(function (error) {
            dump(error);
        });
    },
    afterSubmitForm(row,res){
        let _that = this;
        if(res.dialogIsUpdate){
            this.$confirm('该编号产品属性有改动,请确认产品档案操作方式?', '提示', {
                confirmButtonText: '新增',
                cancelButtonText: '修改',
                closeOnClickModal:false,
                type: 'warning'
            }).then(() => {
                _that.row.isProductUpdate = 'add';
                // 新增的情况，去掉原来的产品id
                _that.row.productId = '';
                if(!_that.row.id)_that.row.code = '';
                _that.submitForm('mainForm');
            }).catch(() => {
                 _that.row.isProductUpdate = 'update';
                 _that.submitForm('mainForm');
            })
        }else{
            var url = res.targetUrl ? res.targetUrl : this.targetUrl;
            if(url) {
                setTimeout(function(){
                    if(_that.row.id){
                        window.location.href = url;
                    }else{
                        window.parent.location.href = url;
                    }
                },this.redirectTime);
            }
        }
    },
    //所有字段变动会去验证条码是否可用
    main:function(){
        var _this = this;
        this.$watch(
            function(){
                let productInfo  = {
                    code:_this.row.code||"",
                    proName:_this.row.proName,
                    gexing:_this.row.gexing,
                    zuzhi:_this.row.zuzhi,
                    proDesc:_this.row.proDesc,
                    type:_this.row.type,
                    hzlId:_this.row.hzlId,
                };
                for(var item of varTemplate.jichuField){
                    productInfo[item.filed] = _this.row[item.filed];
                }
                
                return productInfo;
            },
            function(newVal, oldVal){
                let flag = false;
                let i = 0;
                for(var k in newVal){
                    if(newVal[k]){
                        i++;
                    }
                }
                if(i<=5){
                    return false;
                }
                for(var key in oldVal){
                    if(oldVal[key]){
                        flag = true;
                    }
                }
                if(!flag){
                    return false;
                }
                if(newVal.code==''){
                    return false;
                }
                if(JSON.stringify(newVal)!=JSON.stringify(oldVal)){
                    // 拿新的值到后台验证条码号,及时输出到页面
                    let code = "";
                    let url = ""
                    this.$http.post('?controller=Sample_Ruku&action=CheckCode',{'row':this.row}).then((res)=>{
                        if(!res.data.success) {
                            this.$notify.error({
                              'title':'条码验证失败',
                              'message':res.data.msg ? res.data.msg : '获取产品信息失败'
                            });
                        }else{
                            code = res.data.code;
                        }
                    _this.$set(_this.row , 'code' , code);
                    }).catch(function (error) {
                        dump(error);
                    });
                }
                
            },
            {deep:true,immediate:true}
        );
    }
  }
  return opt;
});